package com.exm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exm.pojo.DataLog;
import com.exm.repository.DataLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DataService {


    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    DataLogRepository repository;

    @Transactional
    public void saveData(double value) {
        DataLog data = new DataLog(value,new Date());
        repository.save(data);
    }


    public List<DataLog> getData() {
        // 获取当前时间
        // 获取当前时间
        Date now = new Date();

        // 获取当前时间的五分钟前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MINUTE, -10);  // 减去5分钟
        Date fiveMinutesAgo = calendar.getTime();

        // 构建查询条件
        QueryWrapper<DataLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("timestamp", fiveMinutesAgo);  // timestamp > 五分钟前


        // 执行查询
        return repository.list(queryWrapper);
    }

}
